package com.lic.system.controller;

import com.lic.system.common.Result;
import com.lic.system.pojo.SysLogin;
import com.lic.system.service.UserService;
import com.lic.system.service.UserTokenService;
import com.lic.system.threadLocal.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author Hewb
 * @ClassName: sysController
 * @Description: TODO
 * @date: 2020/7/11 9:33
 * @since JDK 1.8
 */
@RestController
@RequestMapping("/sys")
public class LoginController {


    @Autowired
    UserService userServcie;


    @Autowired
    UserTokenService userTokenService;


    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody SysLogin sysLogin) {


        SysLogin user = userServcie.queryByUserName(sysLogin.getUserName());
        //账号不存在、密码错误
        if(user == null || !user.getPassWord().equals(sysLogin.getPassWord())) {
            return Result.error("账号或密码不正确");
        }
        //生成token，并保存到数据库
      Result r = userTokenService.createToken(user.getUserId());
        return r;
    }

    /**
     * 退出
     */
    @PostMapping("/sys/logout")
    public Result logout() {
        userTokenService.logout(UserThreadLocal.getUser().getUserId());
        return Result.ok();
    }
}
